package hellogwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class HelloGWTHtml implements EntryPoint {

	private HTML content;// 用于显示动态内容

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// 建立一个StackPanel,将放在左边，显示连接列表
		StackPanel stack = new StackPanel();
		initStack(stack);

		// 建立用于动态显示内容的HTMl部件
		content = new HTML();

		// 清理一下HTML的原始内容
		clean("head");
		clean("list");
		clean("content");

		// 往指定的几个区域添加GWT部件
		RootPanel.get("head").add(new Label("由GWT动态建立的网页"));
		RootPanel.get("list").add(stack);
		RootPanel.get("content").add(content);

	}

	protected void clean(String id) {
		Element element = DOM.getElementById(id);
		DOM.setInnerHTML(element, "");
	}

	protected void initStack(StackPanel stack) {
		VerticalPanel sites = new VerticalPanel();
		VerticalPanel newest = new VerticalPanel();
		VerticalPanel about = new VerticalPanel();

		stack.setWidth("100%");
		stack.add(sites, "内容列表");
		stack.add(newest, "最新介绍");
		stack.add(about, "关于");

		sites.add(createLink("介绍GWT","如果要快速了解<B>GWT</B>,还是看http://www.smartdio.com 吧"));
		sites.add(createLink("详细GWT","详细了解<B>GWT</B>,那就看GWT官方网站"));
		sites.add(createLink("要用GWT","去官方网站下载吧"));
		
		newest.add(createLink("查看最新内容","Sorry,没有！"));
		about.add(createLink("关于这个程序","这是一个实例，演示如何在HTML中插入GWT建立的内容。"));

	}

	protected Hyperlink createLink(String text, final String message) {
		Hyperlink link = new Hyperlink();
		link.setText(text);
		link.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				content.setHTML(message);
			}

		});
		return link;
	}

}
